import Vue from 'vue'
import App from './App.vue'
import { post } from "../config/axios.js"                   //引入调用api的方法
import { api } from "../config/env.js"                             //引入api链接
import VueTouch from 'vue-touch'                                  //引入VueTouch组件 组件的各种响应事件
import store from './store'                                       //引入vuex组件
import router from "./router/index.js"                            //引入路由组件
import VueSwiper from 'vue-awesome-swiper'                        //引入swiper组件
import 'swiper/dist/css/swiper.css'                               //引入swiper的依赖css
import './common/stylus/base.styl'                                //引入全局css动画样式
import music from './common/js/music.js'                          //引入music音乐控制文件
import until from './common/js/until.js'                          //引入until公共方法文件
import variable from './common/js/BaseVariable.js'                //引入variable全局变量
import LoadFeature from './common/js/load.js'                     //引入图片缓存方法
import lottie from 'vue-lottie'                                   //引入lottie动画
Vue.component('lottie', lottie)                                   //注册lottie全局组件
Vue.config.productionTip = false
const vuePrototype = Vue.prototype
vuePrototype.$post = post
vuePrototype.api = api
vuePrototype.variable = variable
vuePrototype.LoadFeature = LoadFeature
vuePrototype.deviceUuid = vuePrototype.variable.DEVICE_UUID

//注入组件
Vue.use(VueTouch, { name: 'v-touch' })
Vue.use(VueSwiper)
Vue.use(music)
Vue.use(until)
//监听用户返回键 直接退出app
var neededToExitOnBack = true
document.addEventListener("backbutton", event => {
  if (neededToExitOnBack) navigator.app.exitApp()
})
document.addEventListener('resume', () => {
  setTimeout(() => { neededToExitOnBack = true }, 1000)
}, false)
//监听用户home键 停止音乐
document.addEventListener('pause', () => {
  neededToExitOnBack = false
  vuePrototype.diologMusic({
    play: false,
    volume: 1
  })
  vuePrototype.parseTextAudio({
    play: false
  })
  vuePrototype.picbookAudioMusic({
    play: false
  })
}, false)
//路由守卫
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth && !vuePrototype.variable.isMp) { // 判断该路由是否需要登录权限
    JSON.parse(localStorage.getItem('isLogin')) ? next() : next({ path: '/login' }) //判断本地是否存在access_token
  }
  else {
    next()
  }
  if (JSON.parse(localStorage.getItem('isLogin')) || vuePrototype.variable.isMp) { //不允许直接跳转到其他页面
    to.path !== "/recommendList" && from.path === "/" ? next({ path: "/recommendList" }) : next({ path: to.path, query: to.query })
  }
  else {
    next()
  }
})
new Vue({
  store,
  router,
  render: h => h(App)
}).$mount('#app')
